/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-03-28
 * Time: 11:40
 */
// 2、无头双向链表实现

public class myLinkedList {

    static class Node {
        public int val;//数据域
        public Node prev;//前驱
        public Node next;//后继

        //构造方法
        public Node(int val) {
            this.val = val;
        }

    }

    public Node head;//头
    public Node last;//尾


    //头插法
    public void addFirst(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = last = node;
        } else {
            node.next = this.head;
            head.prev = node;
            head = node;
        }
    }

    //尾插法
    public void addLast(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = last = node;
        } else {
            last.next = node;
            node.prev = last;
            last = node;
        }

    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) {
        Node node = new Node(data);
        //index合法性
        try {
            if (index < 0 || index > size()) {
                throw new IndexNotLegalException("index不合法");
            }

        } catch (IndexNotLegalException e) {
            e.printStackTrace();
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        //
        Node cur = head;
        while (index != 0) {
            index--;
            cur = cur.next;
        }
        node.next = cur;
        node.prev = cur.prev;
        cur.prev.next = node;
        cur.prev = node;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key) {
        Node del = head;
        if (head == null) {
            return;
        }
        if (head.next == null) {
            if (head.val == key) {
                this.last = null;
                this.head = null;
                return;
            }
        }
        while (del != null) {
            if (del.val == key) {
                break;
            }
            del = del.next;
        }
        if (del == null) {
            return;
        }
        //
        if (del == last) {
            last = last.prev;
            del.prev = del.next = null;
            last.next = null;
            return;
        }
        if (del == head) {
            head = head.next;
            head.prev = null;
            del.prev = del.next = null;
            return;
        }
        Node pre = del.prev;
        Node next = del.next;
        pre.next = next;
        next.prev = pre;
        del.prev = del.next = null;
    }

    //删除所有值为key的节点
    public void removeAllKey(int key) {
        //如果前面都是key
        while (head.val == key) {

            if (head.next == null) {
                //只剩下一个结点了
                head = last = null;
                return;
            }
            head = head.next;
            head.prev = null;
        }
        //如果后面都是key
        while (last.val == key) {
            last = last.prev;
            last.next = null;
        }

        Node del = head;
        //中间的key
        while (del.next != null) {
            if (del.val == key) {
                Node pre = del.prev;
                Node next = del.next;

                pre.next = next;
                next.prev = pre;
                del.prev = null;
                del.next = null;

                del = pre;
            }
            del = del.next;
        }

    }

    //得到单链表的长度
    public int size() {
        int size = 0;
        Node cur = head;
        while (cur != null) {
            cur = cur.next;
            size++;
        }
        return size;
    }

    public void display() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    public void clear() {
        Node cur = head;
        while (cur != null) {
            Node next = cur.next;
            cur.prev = cur.next = null;
            cur = next;
        }
    }
}
